package com.wlw.layoutmanager.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;

import com.wlw.layoutmanager.R;
import com.wlw.layoutmanager.adapters.ViewPagerLayoutManagerAdapter;
import com.wlw.layoutmanager.beans.VideoBean;
import com.wlw.layoutmanager.layoutManager.OnViewPagerListener;
import com.wlw.layoutmanager.layoutManager.ViewPagerLayoutManager;

import java.util.ArrayList;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class ViewPagerLayoutManagerActivity extends SwipeBackActivity implements OnViewPagerListener {

    private RecyclerView mRecyclerView;
    private ViewPagerLayoutManager mLayoutManager;
    private ArrayList<VideoBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_layout_manager);
        initView();
    }

    private void initView() {

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mLayoutManager = new ViewPagerLayoutManager(this);
        mLayoutManager.setOnViewPagerListener(this);
        ViewPagerLayoutManagerAdapter adapter = new ViewPagerLayoutManagerAdapter(0, initData());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(mLayoutManager);
    }

    private ArrayList<VideoBean> initData() {
        int[] imgs = {R.mipmap.bg_1, R.mipmap.bg_2, R.mipmap.img_video_1, R.mipmap.img_video_2};
        int[] videos = {R.raw.video_3, R.raw.video_4, R.raw.video_1, R.raw.video_2};
        data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            data.add(new VideoBean(imgs[i % 4], videos[i % 4]));
        }
        return data;
    }

    @Override
    public void onInitComplete() {
        playVideo(0);
    }

    @Override
    public void onPageRelease(boolean isNext, int position) {
        int index;
        if (isNext) {
            index = 0;
        } else {
            index = 1;
        }
        releaseVideo(index);
    }

    @Override
    public void onPageSelected(int position, boolean isBottom) {
        playVideo(position);
    }

    private void playVideo(int position) {
        View itemView = mRecyclerView.getChildAt(0);
        final VideoView videoView = itemView.findViewById(R.id.video_view);
        final ImageView imgPlay = itemView.findViewById(R.id.img_play);
        final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
        final MediaPlayer[] mediaPlayers = new MediaPlayer[1];
        videoView.start();
        videoView.setOnInfoListener((mp, what, extra) -> {
            mediaPlayers[0] = mp;
            mp.setLooping(true);
            imgThumb.animate().alpha(0).setDuration(200).start();
            return false;
        });
        imgPlay.setOnClickListener(it -> {
            if (videoView.isPlaying()) {
                imgPlay.animate().alpha(1f).start();
                videoView.pause();
            } else {
                imgPlay.animate().alpha(0f).start();
                videoView.start();
            }
        });
    }

    private void releaseVideo(int index) {
        View itemView = mRecyclerView.getChildAt(0);
        final VideoView videoView = itemView.findViewById(R.id.video_view);
        final ImageView imgThumb = itemView.findViewById(R.id.img_thumb);
        final ImageView imgPlay = itemView.findViewById(R.id.img_play);
        videoView.stopPlayback();
        imgThumb.animate().alpha(1).start();
        imgPlay.animate().alpha(0f).start();
    }
}
